package com.clgc.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.clgc.eduservice.entity.EduTask;
import com.clgc.eduservice.entity.vo.Kcbj;
import com.clgc.eduservice.entity.vo.KcbjVo;
import com.clgc.eduservice.mapper.EduTaskMapper;
import com.clgc.eduservice.service.EduTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clgc.eduservice.service.KcbjTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 作业表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-27
 */
@Service
@Slf4j
public class EduTaskServiceImpl extends ServiceImpl<EduTaskMapper, EduTask> implements EduTaskService {


    @Autowired
    KcbjTaskService kcbjTaskService;

    public List<EduTask> selects(HashMap<String, String> term) {
        QueryWrapper<EduTask> queryWrapper = new QueryWrapper<EduTask>();
        queryWrapper.eq("teacher_id", term.get("teacher_id"))
                .eq(term.get("course_id") != null, "course_id", term.get("course_id"))
                .eq(term.get("kind") != null, "kind", term.get("kind"))
                .like(term.get("title") != null, "title", term.get("title"))
                .ge(term.get("submit_time") != null, "submit_time", term.get("submit_time"))
                .between(term.get("start_time") != null && term.get("end_time") != null,
                        "create_time", term.get("start_time"), term.get("end_time"));
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List getKcbjTaskByCourseId(String courseId) {

        QueryWrapper<EduTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "title", "kind", "status", "create_time");
        queryWrapper.eq("course_id", courseId);
        List<EduTask> taskList = baseMapper.selectList(queryWrapper);
        List<KcbjVo> kcbjVoList = new ArrayList<>();

        for (EduTask eduTask : taskList) {
            List<Kcbj> kcbjList = kcbjTaskService.getKcbjByTaskId(eduTask.getId());
            if (kcbjList.size() != 0) {
                KcbjVo kcbjVo = new KcbjVo();
                BeanUtils.copyProperties(eduTask, kcbjVo);
                kcbjVo.setListKcbj(kcbjList);
                kcbjVoList.add(kcbjVo);
            }
        }

        return kcbjVoList;
    }

    @Resource
    private EduTaskMapper eduTaskMapper;

    /**
     * 插入
     *
     * @param eduTask
     * @return
     */
    public int insert(EduTask eduTask) {
        int result = eduTaskMapper.insert(eduTask);
        return result;
    }

    /**
     * 更新作业
     *
     * @param eduTask
     * @return
     */
    public int update(EduTask eduTask) {
        int result = eduTaskMapper.updateById(eduTask);
        return result;
    }

    /**
     * 根据id批量删除
     *
     * @param ids
     * @return
     */
    public int deleteByIds(ArrayList<String> ids) {
        return eduTaskMapper.deleteBatchIds(ids);

    }
}
